<template>
  <div class="login">
    <MyNavbar path="/" title="登录"></MyNavbar>
    <!-- <MyNavbar path="/" :left="true" title="中间标题" right="提交"> ></MyNavbar> -->
    <!-- <van-nav-bar left-text="返回" left-arrow @click-left="onClickLeft" /> -->
    <div class="content">
      <h3 class="login-title">您好，请登录</h3>
      <van-form ref="form">
        <van-field
          :rules="rules.mobile"
          v-model="form.mobile"
          name="mobile"
          placeholder="请输入手机号"
        >
          <template #left-icon>
            <i class="iconfont r20">&#xe60b;</i>
          </template></van-field
        >
        <van-field
          :rules="rules.code"
          v-model="form.code"
          name="code"
          placeholder="请输入验证码"
        >
          <template #left-icon>
            <i class="iconfont r20">&#xe60c;</i>
          </template>
          <template #button>
            <span class="code" v-if="timeLeft === 6" @click="sendCode">
              获取验证码
            </span>
            <span class="code" v-else>{{ timeLeft }}后重试</span>
          </template></van-field
        >
      </van-form>
      <div class="tip">
        <span>登录即同意</span><span class="color">《用户使用协议》</span
        ><span>和</span><span class="color">《隐私协议》</span>
      </div>
      <br />
      <div class="btn">
        <van-button block color="#E40137" round @click="submit"
          >确定</van-button
        >
      </div>
      <br />
      <!-- <div>
        <van-button plain hairline block round type="primary" @click="btnTest"
          >测试按钮</van-button
        >
      </div> -->
    </div>
  </div>
</template>

<script>
import { auCodePost, auLoginPost, auInfoGet } from '@/api/login.js'
import { setLocal } from '@/utils/auth.js'
export default {
  name: 'Login',
  data () {
    return {
      timeLeft: 6,
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'onChange' },
          {
            validator: value => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '请正确输入手机号',
            trigger: 'onChange'
          }
        ],
        code: [
          { required: true, message: '请输入4位验证码', trigger: 'onChange' },
          {
            validator: value => {
              return value.length === 4
            },
            message: '请输入4位验证码',
            trigger: 'onChange'
          }
        ]
      }
    }
  },
  created () {
    // console.log('route', this.$route.query)
  },
  methods: {
    onClickLeft () {
      this.$router.push('/')
    },
    // 测试按钮
    async btnTest () {
      const res = await auInfoGet()
      console.log('auInfo:', res)
      console.log('store中的用户信息', this.$store.state.userInfo)
    },
    // 发送验证码，先验证手机号
    sendCode () {
      this.$refs.form
        .validate('mobile')
        .then(async () => {
          // 验证通过就减一秒,避免重复点击
          this.timeLeft--
          const _interval = setInterval(() => {
            this.timeLeft--
            if (this.timeLeft <= 0) {
              clearInterval(_interval)
              // 重置计时器,恢复按钮显示
              this.timeLeft = 6
            }
          }, 1000)
          const res = await auCodePost({ mobile: this.form.mobile })
          console.log('res', res)
          // this.form.code = res.data
          this.$toast.success('验证码是' + res.data)
        })
        .catch(() => {
          this.$toast.fail('验证失败')
        })
    },
    // 登录接口，先完成全局表单验证
    submit () {
      let res = {}
      this.$refs.form
        .validate()
        .then(async () => {
          res = await auLoginPost(this.form)
          // 保存token到localStorage
          setLocal('userToken', res.data.jwt)
          // 保存用户信息
          this.$store.commit('setUserInfo', res.data.user)
        })
        .catch(() => {
          this.$toast.fail('验证失败')
        })
      // 从哪里跳转到登录页,登陆后跳转会对应页面
      const _redirect = this.$route.query.redirect
      // console.log('route', this.$route)
      if (_redirect) {
        this.$router.push(_redirect)
      } else {
        this.$router.push('/')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  // h3.title {
  //   font-size: 44px;
  //   margin: 65px 50px;
  // }
  background-color: #fff;
  .content {
    padding: 0 30px;
    .login-title {
      margin: 50px 0 65px;
      font-size: 44px;
    }
    .r20 {
      padding-right: 20px;
    }
    .code {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: center;
      color: #00b8d4;
      line-height: 22px;
      letter-spacing: 0px;
    }
    .tip {
      font-size: 12px;
      margin-top: 27px;
      margin-bottom: 44px;
      .color {
        color: #00b8d4;
      }
    }
  }
}
</style>
